home *** CD-ROM | disk | FTP | other *** search
- Path: scopevle.demon.co.uk!gsmith
- From: "Gary J. Smith" <gsmith@scopevle.demon.nl>
- Newsgroups: comp.lang.c++
- Subject: Re: HELP: Overloading [] operator for multidimensional array of objects.
- Date: Wed, 3 Apr 1996 16:56:40 +0100
- Organization: ScopeVale Ltd. (Amsterdam)
- Distribution: world
- Message-ID: <o+xjVJA4+pYxEwrx@scopevle.demon.co.uk>
- References: <4j6p2v$dhg@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: scopevle.demon.co.uk
- X-NNTP-Posting-Host: scopevle.demon.co.uk
- MIME-Version: 1.0
- X-Newsreader: Turnpike (evaluation) Version 1.12 <8zaiLiZKAVASB6YSBmEMpYSBBk>
-
- In article <4j6p2v$dhg@vixen.cso.uiuc.edu>, "Edgar L. Torres" <e-
- torres@uiuc.edu> writes
- >Hi!
- >
- > I would like to specify the overload of [], such that I may access
- >a multidimensional array of objects. As an example, given I have a
- >class
- >EXAMCLASS and I wish to dynamically allocate a three-dimensional array
- >of
- >objects:
- > EXAMCLASS object1***;
- > ...
- > object1 = malloc( sizeof(object1), dim1sz * dim2sz * dim3sz );
- >I know the above is sacrilege to C++ purists, but bare with me, since
- >the malloc
- >does work correctly in C++. Now, what I want is to be able to:
- > object1[arg1][arg2][arg3].obj1Var1 = something;
- >OR
- > something = object1[arg1][arg2][arg3].obj1Var1;
- >In general, I know I have to include three friend operator in the
- >definition of
- >class EXAMCLASS, and then define them outside:
- > EXAMCLASS** operator[](EXAMCLASS*** C, int i){
- > return ((EXAMCLASS **)(C + (i * dim2sz * dim3sz))); }
- > EXAMCLASS* operator[](EXAMCLASS** C, int i){
- > return ((EXAMCLASS *)(C + (i * dim3sz))); }
- > EXAMCLASS operator[](EXAMCLASS* C, int i){
- > return *(C + i); }
- >I know that the above does not work, but it does present the basic
- >idea of what
- >I am trying to do. If anybody has the knowledge to help with the
- >specifics of
- >syntax and argument rules, please HELP ME! Somebody suggested
- >replacing the *
- >with &, but I am not sure what EXAMCLASS&& means??
- >
- >Thanks in advance,
- >Edgar
- >
- >
-
- As a short cut, why not overload the () operator - it can take as many (
- and differing) parameter types as you want ? This can then return a
- reference to something that you can amend.
-
- (Maybe I missed the point, but I had this problem so this is what I
- did).
-
- Gary.
-
- ---------------------------------------------------
- Gary J. Smith
- gsmith@scopevle.demon.co.uk
- gsmith@scopevle.demon.nl
-
- ---------------------------------------------------
-
- Turnpike evaluation. For information, see http://www.turnpike.com/
-